﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CodeBuilderBase
{
    public class CodeBuild
    {
        public string GetCSharpIntTypeFromSql(string sqlIntType)
        {
            string returnType = "int";
            switch (sqlIntType.ToUpper())
            {
                case "INT":
                    {
                        returnType = "int";
                    }
                    break;
                case "BIGINT":
                    {
                        returnType = "Int64";
                    }
                    break;
                case "TINYINT":
                    {
                        returnType = "Int16";
                    }
                    break;
                default:
                    {
                        returnType = "Int64";
                    }
                    break;
            }
            return returnType;
        }

        /// <summary>
        /// 生成DAlLinqCode代码
        /// </summary>
        /// <param name="dicList"></param>
        /// <param name="exclusive">不包含的KEY</param>
        /// <returns></returns>
        public string DAlLinqCode(string nameSpace, string className, string tableObjectName, KeyValuePair<string, string> key, string DataContextName)
        {
            string cSharpIntType = GetCSharpIntTypeFromSql(key.Value);
            string keyName = key.Key;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Linq;");
            sb.AppendLine("using System..Data.Linq;");
            sb.AppendLine();
            sb.AppendLine("namespace " + nameSpace + "");
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// " + className + "操作类");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    public static class " + className + "");
            sb.AppendLine("    {");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 载入一个实体");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"id\">标识符列</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        public static " + tableObjectName + " Load(this " + tableObjectName + " prd," + cSharpIntType + " id)");
            sb.AppendLine("        {");
            sb.AppendLine("            try");
            sb.AppendLine("            {");
            sb.AppendLine("                using (" + DataContextName + " objDataContext = new " + DataContextName + "())");
            sb.AppendLine("                {");
            sb.AppendLine("                    return (from objData in objDataContext." + tableObjectName + "");
            sb.AppendLine("                            where objData."+ keyName +" == id");
            sb.AppendLine("                            select objData).SingleOrDefault();");
            sb.AppendLine("                }");
            sb.AppendLine("            }");
            sb.AppendLine("            catch(Exception ex)");
            sb.AppendLine("            {");
            sb.AppendLine("                throw ex;");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 保存一个实体");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"localRecord\">" + tableObjectName + "实体</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        public static " + tableObjectName + " Save(this " + tableObjectName + " localRecord)");
            sb.AppendLine("        {");
            sb.AppendLine("            if (localRecord."+ keyName +" > 0)");
            sb.AppendLine("            {");
            sb.AppendLine("                return UpdateRecord(localRecord);");
            sb.AppendLine("            }");
            sb.AppendLine("            else");
            sb.AppendLine("            {");
            sb.AppendLine("                return InsertRecord(localRecord);");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 增加一个实体");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"localRecord\">" + tableObjectName + "实体</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        private static "+ tableObjectName +" InsertRecord(" + tableObjectName + " localRecord)");
            sb.AppendLine("        {");
            sb.AppendLine("            bool flag = false;");
            sb.AppendLine("            try");
            sb.AppendLine("            {");
            sb.AppendLine("                using (" + DataContextName + " objDataContext = new " + DataContextName + "())");
            sb.AppendLine("                {");
            sb.AppendLine("                    objDataContext." + tableObjectName + ".InsertOnSubmit(localRecord);");
            sb.AppendLine("                    objDataContext.SubmitChanges();");
            sb.AppendLine("                    flag = true;");
            sb.AppendLine("                }");
            sb.AppendLine("            }");
            sb.AppendLine("            catch");
            sb.AppendLine("            {");
            sb.AppendLine("            }");
            sb.AppendLine("            return flag ? localRecord : null;");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 更新一个实体");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"localRecord\">" + tableObjectName + "实体</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        private static " + tableObjectName + " UpdateRecord(" + tableObjectName + " localRecord)");
            sb.AppendLine("        {");
            sb.AppendLine("            bool flag = false;");
            sb.AppendLine("            try");
            sb.AppendLine("            {");
            sb.AppendLine("                using (" + DataContextName + " objDataContext = new " + DataContextName + "())");
            sb.AppendLine("                {");
            sb.AppendLine("                    objDataContext." + tableObjectName + ".Attach(localRecord);");
            sb.AppendLine("                    objDataContext.Refresh(RefreshMode.KeepCurrentValues, localRecord);");
            sb.AppendLine("                    objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);");
            sb.AppendLine("                    flag = true;");
            sb.AppendLine("                }");
            sb.AppendLine("            }");
            sb.AppendLine("            catch");
            sb.AppendLine("            {");
            sb.AppendLine("            }");
            sb.AppendLine("            return flag ? localRecord : null;");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 删除一个实体");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"id\">标识符列</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        public static bool Delete(this " + tableObjectName + " prd," + cSharpIntType + " id)");
            sb.AppendLine("        {");
            sb.AppendLine("            bool flag = false;");
            sb.AppendLine("            try");
            sb.AppendLine("            {");
            sb.AppendLine("                using (" + DataContextName + " objDataContext = new " + DataContextName + "())");
            sb.AppendLine("                {");
            sb.AppendLine("                    objDataContext." + tableObjectName + ".DeleteOnSubmit((from objData in objDataContext." + tableObjectName + "");
            sb.AppendLine("                                                           where objData." + keyName + " == id");
            sb.AppendLine("                                                           select objData).SingleOrDefault());");
            sb.AppendLine("                   objDataContext.SubmitChanges();");
            sb.AppendLine("                   flag = true;");
            sb.AppendLine("                }");
            sb.AppendLine("            }");
            sb.AppendLine("            catch");
            sb.AppendLine("            {");
            sb.AppendLine("            }");
            sb.AppendLine("            return flag;");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            return sb.ToString();
        }
    }
}